﻿Imports FinanceiroDAL
Public Class BancosBLL
    Dim objDAL As New BancosDAL
#Region "Variáveis de Instância e propriedades"
    Private banCodigo As Integer
    Public Property pbanCodigo() As Integer
        Get
            Return banCodigo
        End Get
        Set(ByVal value As Integer)
            banCodigo = value
        End Set
    End Property
    Private banNome As String
    Public Property pbanNome() As String
        Get
            Return banNome
        End Get
        Set(ByVal value As String)
            banNome = value
        End Set
    End Property
    Private banEndereco As String
    Public Property pbanEndereco() As String
        Get
            Return banEndereco
        End Get
        Set(ByVal value As String)
            banEndereco = value
        End Set
    End Property
    Private banBairro As String
    Public Property pbanBairro() As String
        Get
            Return banBairro
        End Get
        Set(ByVal value As String)
            banBairro = value
        End Set
    End Property
    Private banCidade As String
    Public Property pbanCidade() As String
        Get
            Return banCidade
        End Get
        Set(ByVal value As String)
            banCidade = value
        End Set
    End Property
    Private banAgencia As String
    Public Property pbanAgencia() As String
        Get
            Return banAgencia
        End Get
        Set(ByVal value As String)
            banAgencia = value
        End Set
    End Property
    Private banTelefone1 As Integer
    Public Property pbanTelefone1() As Integer
        Get
            Return banTelefone1
        End Get
        Set(ByVal value As Integer)
            banTelefone1 = value
        End Set
    End Property
    Private banTelefone2 As Integer
    Public Property pbanTelefone2() As Integer
        Get
            Return banTelefone2
        End Get
        Set(ByVal value As Integer)
            banTelefone2 = value
        End Set
    End Property
    Private banCelular As Integer
    Public Property pbanCelular() As Integer
        Get
            Return banCelular
        End Get
        Set(ByVal value As Integer)
            banCelular = value
        End Set
    End Property
    Private banCEP As String
    Public Property pbanCEP() As String
        Get
            Return banCEP
        End Get
        Set(ByVal value As String)
            banCEP = value
        End Set
    End Property
    Private banUf As String
    Public Property pbanUf() As String
        Get
            Return banUf
        End Get
        Set(ByVal value As String)
            banUf = value
        End Set
    End Property
    Private banContato As String
    Public Property pbanContato() As String
        Get
            Return banContato
        End Get
        Set(ByVal value As String)
            banContato = value
        End Set
    End Property
    Private banHomePage As String
    Public Property pbanHomePage() As String
        Get
            Return banHomePage
        End Get
        Set(ByVal value As String)
            banHomePage = value
        End Set
    End Property
    Private banEndNum As String
    Public Property pbanEndNum() As String
        Get
            Return banEndNum
        End Get
        Set(ByVal value As String)
            banEndNum = value
        End Set
    End Property
    Private banTipo As String
    Public Property pbanTipo() As String
        Get
            Return banTipo
        End Get
        Set(ByVal value As String)
            banTipo = value
        End Set
    End Property
    Private status As String
    Public Property pstatus() As String
        Get
            Return status
        End Get
        Set(ByVal value As String)
            status = value
        End Set
    End Property
#End Region
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New BancosDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.banCodigo = Integer.Parse(tab.Rows(0)("ban_codigo").ToString)
                Me.banNome = tab.Rows(0)("ban_descricao").ToString
                Me.banEndereco = tab.Rows(0)("ban_endereco").ToString
                Me.banBairro = tab.Rows(0)("ban_bairro").ToString
                Me.banCidade = tab.Rows(0)("ban_cidade").ToString
                Me.banAgencia = tab.Rows(0)("ban_agencia").ToString
                Me.banTelefone1 = tab.Rows(0)("ban_telefone1").ToString
                Me.banTelefone2 = tab.Rows(0)("ban_telefone2").ToString
                Me.banCelular = tab.Rows(0)("ban_celular").ToString
                Me.banCEP = tab.Rows(0)("ban_cep").ToString
                Me.banUf = tab.Rows(0)("est_uf").ToString
                Me.banContato = tab.Rows(0)("ban_contato").ToString
                Me.banHomePage = tab.Rows(0)("ban_homepage").ToString
                Me.banEndNum = tab.Rows(0)("ban_numero").ToString
                Me.banTipo = tab.Rows(0)("ban_tipo").ToString
                Me.status = tab.Rows(0)("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Construtores"
    Sub New()
    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(ByVal codigo As Integer, ByVal descr As String, ByVal stacod As Integer)
        Me.pbanCodigo = codigo
        Me.pbanNome = descr
        Me.pstatus = stacod
    End Sub
#End Region
#Region "Metodos"
    Sub limpar()
        Me.pbanCodigo = Nothing
        Me.pbanNome = Nothing
        Me.pbanEndereco = Nothing
        Me.pbanBairro = Nothing
        Me.pbanCidade = Nothing
        Me.pbanAgencia = Nothing
        Me.pbanTelefone1 = Nothing
        Me.pbanTelefone2 = Nothing
        Me.pbanCelular = Nothing
        Me.pbanCEP = Nothing
        Me.pbanUf = Nothing
        Me.pbanContato = Nothing
        Me.pbanHomePage = Nothing
        Me.pbanEndNum = Nothing
        Me.pbanTipo = Nothing
        Me.pstatus = Nothing
    End Sub
#End Region
#Region "inserir"
    Function inserir() As Integer
        Dim ret As Boolean
        Try
            objDAL = New BancosDAL
            If (Trim(pbanNome <> "")) Then
                If (objDAL.ValidaReplica(0, Me.pbanNome, Me.pbanAgencia)) Then
                    Me.pbanCodigo = objDAL.inserir(Me.banNome, Me.banEndereco, Me.banBairro, Me.banCidade, Me.banAgencia, Me.banTelefone1, Me.banTelefone2, Me.banCelular, Me.banCEP, Me.banUf, Me.banContato, Me.banHomePage, Me.banEndNum, Me.banTipo, Me.status)
                Else : Throw New Exception("Inserção não realizada, verifique duplicidade!")
                End If
            Else : Throw New Exception("Favor preencher o nome do campo!")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Boolean
        Try
            objDAL = New BancosDAL
            '    If (Me.status <> 2 Or Me.status <> 3) Then
            If (objDAL.validaExclusao(Me.banCodigo, Me.status)) Then
                If (banAgencia <> "") Then
                    If (objDAL.ValidaReplica(Me.pbanCodigo, Me.pbanNome, Me.pbanAgencia)) Then
                        ret = objDAL.alterar(Me.banNome, Me.banEndereco, Me.banBairro, Me.banCidade, Me.banAgencia, Me.banTelefone1, Me.banTelefone2, Me.banCelular, Me.banCEP, Me.banUf, Me.banContato, Me.banHomePage, Me.banEndNum, Me.banTipo, Me.status, Me.banCodigo)
                    Else : Throw New Exception("Banco já cadastrado")
                    End If
                Else : MsgBox("Verifique os campos, operação não pode ser realizada", MsgBoxStyle.Exclamation)
                End If

            Else
                MsgBox("Banco: " & Me.banNome & " não pode ser alterado/excluído. Verifique a dependência com uma conta!", MsgBoxStyle.Exclamation)
                Return False
            End If
            '    End If
        Catch ex As Exception
            Throw New Exception("Alteração não realizada!")
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Localizar"
    Function localizar() As DataTable 'localizar do consulta
        Dim tab As DataTable
        Try
            objDAL = New BancosDAL
            tab = objDAL.localizar()
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function
    Function localizar(ByRef nome As String) As DataTable 'localizar por nome
        Dim tab As DataTable
        Try
            objDAL = New BancosDAL
            tab = objDAL.localizar(nome)

        Catch ex As Exception
            Throw New Exception("Dados não encontrados!")
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function
    Function localizaAgencia(ByVal agencia As String) As DataTable 'localizar por agencia
        Dim tab As DataTable
        Try
            objDAL = New BancosDAL
            tab = objDAL.localizarAgencia(agencia)

        Catch ex As Exception
            Throw New Exception("Dados não encontrados!")
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function
    Function localizarAgStatus(ByVal status As String) As DataTable 'localizar por agencia
        Dim tab As DataTable
        Try
            objDAL = New BancosDAL
            tab = objDAL.localizarAgStatus(status)
        Catch ex As Exception
            Throw New Exception("Dados não encontrados!")
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function
#End Region
End Class
